import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_appgetwe/page/addvideo/add_video_sccreen.dart';
import 'package:flutter_appgetwe/provider/LoginProvider.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';

// ignore: use_key_in_widget_constructors
class AddVideo extends StatefulWidget {
  @override
  _AddVideoState createState() => _AddVideoState();
}

class _AddVideoState extends State<AddVideo> {
    pickVideo(ImageSource src, BuildContext context) async {
    // ignore: unused_local_variable
    final video = await ImagePicker().getVideo(source: src);
    if (video != null) {
      Navigator.of(context).push(MaterialPageRoute(
          builder: (context) => AddVideoScreen(
                videoFile: File(video.path),
                videoPath: video.path,
              )));
    }
    print("打印视频路径...");
    print(video?.path.toString());
    //将视频路径存入状态
    context.read<LoginProvider>().setVideoPath(video?.path.toString());
  }

  showOptionDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (context) => SimpleDialog(
        children: [
          SimpleDialogOption(
            onPressed: () =>{

              pickVideo(ImageSource.camera, context)
            },
            child: Row(
              children: [
                Icon(Icons.camera_alt),
                Text(
                  "Camera",
                  style: TextStyle(fontSize: 20),
                )
              ],
            ),
          ),
          SimpleDialogOption(
            onPressed: () => pickVideo(ImageSource.gallery, context),
            child: Row(
              children: [
                Icon(Icons.image),
                Text(
                  "Gallery",
                  style: TextStyle(fontSize: 20),
                )
              ],
            ),
          ),
          SimpleDialogOption(
            onPressed: () {},
            child: Row(
              children: [
                Icon(Icons.cancel),
                Text(
                  "Cancel",
                  style: TextStyle(fontSize: 20),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: InkWell(
        onTap: () {
          print("add video");
          showOptionDialog(context);
        },
        child: Container(
          alignment: Alignment.center,
          child: Text(
            "Add Video",
            style: TextStyle(
                fontSize: 18, fontWeight: FontWeight.w400, color: Colors.black),
          ),
          width: 150,
          height: 50,
          decoration: BoxDecoration(
              color: Colors.redAccent[200],
              borderRadius: BorderRadius.circular(9)),
        ),
      ),
    );
  }
}
